import tkinter
import tkinter.messagebox
from login import *

def register():
  HOST = "0.0.0.0"
  PORT = 8002
  s = socket()
  
  print("注册操作")
  root1 = Toplevel()
  root1.title("欢迎来到娱乐世界")
  varName = tkinter.StringVar()
  varName.set('')
  varPwd = tkinter.StringVar()
  varPwd.set('')
  varPwd1 = tkinter.StringVar()
  varPwd1.set('')
  # 设置窗口大小

  root1.geometry('500x500+400+150')

  # 创建一个图片管理类
  photo = tkinter.PhotoImage(file="image/register1.png")  # file：t图片路径
  imgLabel = tkinter.Label(root1,image=photo, compound=tkinter.CENTER, bg="#FF9D28")  # 把图片整合到标签类中
  imgLabel.pack(side=tkinter.LEFT)  # 自动对齐
  imgLabel.place(x=0, y=0)
  def pwdTest():
      if entryName.get() == '':
          tkinter.messagebox.showerror(title='warner', message='用户不能为空')

  def test():
      if entryName.get() == '':
          tkinter.messagebox.showerror(title='warner', message='用户不能为空')
  def pwdTest1():
      if entryPwd.get() == '': 
          tkinter.messagebox.showerror(title='warner', message='密码不能为空')
      # 创建标签
  labelName = tkinter.Label(
      root1, text='用 户 名:', justify=tkinter.RIGHT, width=80, font=("微软雅黑", 14), bg="#FF9D28")
  # 将标签放到窗口上
  labelName.place(x=60, y=200, width=90, height=20)

  # 创建文本框，同时设置关联的变量
  entryName = tkinter.Entry(root1, width=100, textvariable=varName,
                            validate="focusout",bg='#E0E0C4', validatecommand=test)
  entryName.place(x=160, y=195, width=200, height=35)


  labelPwd = tkinter.Label(
      root1, text='密    码:', justify=tkinter.RIGHT, width=80, font=("微软雅黑", 14), bg="#FF9D28")
  labelPwd.place(x=60, y=250, width=90, height=20)

  # 创建密码文本框
  entryPwd = tkinter.Entry(root1, show='*', width=100,textvariable=varPwd,
      validate="focusin",bg='#E0E0C4', validatecommand=pwdTest)
  entryPwd.place(x=160, y=245, width=200, height=35)

  labelPwd1 = tkinter.Label(
      root1, text='确认密码:', justify=tkinter.LEFT, width=80, font=("微软雅黑", 14), bg="#FF9D28")
  labelPwd1.place(x=45, y=300, width=110, height=20)
  # 创建第二个密码文本框
  entryPwd1 = tkinter.Entry(root1, show='*', width=100,textvariable=varPwd1,
       validate="focusin",bg='#E0E0C4', validatecommand=pwdTest1)
  entryPwd1.place(x=160, y=295, width=200, height=35
     )

  def exit():
    root1.destroy()

  def cancel():
    # 清空用户输入的用户名和密码
    varName.set('')
    varPwd.set('')
    varPwd1.set('')

  def do_register():
    HOST = "0.0.0.0"
    PORT = 8002
    s = socket()
    s.connect((HOST,PORT))

    name = entryName.get()
    passwd = entryPwd.get()
    passwd1 = entryPwd1.get()
    if passwd != passwd1:
      tkinter.messagebox.showinfo(title='游戏世界', message='密码不一致！')
      print("密码不一致")
    elif (' ' in name) or (' ' in passwd):
      tkinter.messagebox.showinfo(title='游戏世界', message='用户名或密码不许有空格！')
      print('用户名或密码不许有空格')
    else:
      msg = 'R {} {}'.format(name,passwd)
      #发送请求
      s.send(msg.encode())
      #收到回复
      data = s.recv(128).decode()

      if data == "EXISTS":
        tkinter.messagebox.showinfo(title='游戏世界', message='该用户名已存在！')
        print("用户已存在")
      elif data == 'OK':
        tkinter.messagebox.showinfo(title='游戏世界', message='注册成功，请登录！')
    s.close()

  # 创建按钮组件，同时设置按钮事件处理函数
  buttonOk = tkinter.Button(root1, text='注册', bg="#ED810D",font=("微软雅黑", 14),command=do_register)
  buttonOk.place(x=100, y=400, width=75, height=35)
  buttonOk = tkinter.Button(root1, text='重置', bg="#ED810D",font=("微软雅黑", 14), command=cancel)
  buttonOk.place(x=200, y=400, width=75, height=35)

  buttonCancel = tkinter.Button(root1, text='退出', bg="#ED810D",font=("微软雅黑", 14), command=exit)
  buttonCancel.place(x=300, y=400, width=75, height=35)
  # 启动消息循环
  root1.mainloop()
